﻿using System;
using System.Drawing;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;

using SmartParking.Data.Entities;
using SmartParking.Bussiness;

namespace SmartParking.Web.Manager
{
    public partial class Customers : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                (Master as Logger).Title = "Customers";
                (Master as Logger).ActiveMenuItem = "Customers";
                LoadCustomers();
            }
        }

        protected void LoadCustomers()
        {
            Bussiness.ParkingManager.BindCustomers(ref grdCustomer);
            upCustomer.Update();
        }

        protected void btnSubmitCustomer_Click(object sender, EventArgs e)
        {
            Bussiness.ParkingManager.SaveCustomer(
                hfIdCustomer.Value,
                txtName.Text,
                txtEmail.Text,
                txtPhone.Text,
                txtAddress.Text,
                Profile.ManagerID,
                Request.UserHostAddress);

            CloseDialog("pnlCustomerForm");
            CleanCustomerForm();
            LoadCustomers();
        }

        /// <summary>
        /// Registers client script to close the dialog
        /// </summary>
        /// <param name="dialogId"></param>
        private void CloseDialog(string dialogId)
        {
            string script = string.Format(@"closeDialog('{0}')", dialogId);
            ScriptManager.RegisterClientScriptBlock(this, typeof(Page), UniqueID, script, true);
        }

        /// <summary>
        /// Registers client script to close the dialog
        /// </summary>
        /// <param name="dialogId"></param>
        private void OpenDialog(string dialogId)
        {
            string script = string.Format(@"showDialog('{0}')", dialogId);
            ScriptManager.RegisterClientScriptBlock(this, typeof(Page), UniqueID, script, true);
        }

        protected void grdCustomer_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "EditCustomer")
            {
                Customer customer = ParkingManager.GetCustomer(Convert.ToInt32(e.CommandArgument));
                if (customer != null)
                {
                    hfIdCustomer.Value = e.CommandArgument.ToString();
                    txtName.Text = customer.Name;
                    txtAddress.Text = customer.Address;
                    txtEmail.Text = customer.Email;
                    txtPhone.Text = customer.Phone;
                    btnSubmitCustomer.Text = "Update";
                    lblCustomerTitle.Text = "Modify Customer";
                    btnDeleteCustomer.Visible = true;
                    upCustomerForm.Update();
                    OpenDialog("pnlCustomerForm");
                }
            }
            else if (e.CommandName == "DeleteCustomer")
                DeleteCustomer(Convert.ToInt32(e.CommandArgument));
        }

        private void DeleteCustomer(int id)
        {
            ParkingManager.DeleteCustomer(id, Profile.ManagerID, Request.UserHostAddress);
            LoadCustomers();
        }

        protected void btnDeleteCustomer_Click(object sender, EventArgs e)
        {
            DeleteCustomer(Convert.ToInt32(hfIdCustomer.Value));
            CloseDialog("pnlCustomerForm");
            upCustomer.Update();
        }

        protected void btnCancelCustomer_Click(object sender, EventArgs e)
        {
            CleanCustomerForm();
            CloseDialog("pnlCustomerForm");
        }

        private void CleanCustomerForm()
        {
            btnSubmitCustomer.Text = "Save";
            lblCustomerTitle.Text = "Add Customer";
            btnDeleteCustomer.Visible = false;

            txtAddress.Text = txtName.Text = txtPhone.Text = txtEmail.Text = hfIdCustomer.Value = String.Empty;
        }
    }
}
